<template>
  <div class="cart-center">
    <transition name="move">
      <div class="jian" v-show="totalCount > 0" @click="decFood">
        <span
          class="inner iconfont icon-minus-circle"
          style="display: inline-block"
        ></span>
      </div>
    </transition>
    <div class="count" v-show="totalCount > 0">
      {{ totalCount }}
    </div>
    <div
      v-if="selectFoods.length < 2"
      class="add iconfont icon-jiahao"
      @click="addFood()"
    ></div>
    <div v-if="selectFoods.length >= 2">
      <span class="duo" @click="showMore">多规则</span>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    selectFoods: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  computed: {
    totalCount() {
      let count = 0;
      this.selectFoods.forEach((item) => {
        if (item.count) {
          count += item.count;
        }
      });
      return count;
    },
  },
  methods: {
    addFood() {
      if (!this.selectFoods[0].count) {
        this.$set(this.selectFoods[0], "count", 1);
      } else {
        this.selectFoods[0].count++;
      }
    },
    decFood() {
      if (this.selectFoods.length < 2) {
        if (this.selectFoods[0].count) {
          this.selectFoods[0].count--;
        }
      }
    },
    showMore() {
      this.$emit("showMore");
    },
  },
};
</script>

<style lang="scss" scoped>
.duo {
  display: inline-block;
  padding: 0.066667rem 0.133333rem;
  background-color: #3190e8;
  color: #fff;
  font-size: 0.32rem;
  border-radius: 0.15rem;
}
.cart-center {
  display: flex;
  padding-right: 0.32rem;
  position: absolute;
  top: 0;
  right: 0;
  .move-enter-to,
  .move-leave {
    opacity: 1;
    transform: translateX(0);
  }
  .move-enter-active,
  .move-leave-active {
    transition: all 0.5s;
  }
  .move-enter,
  .move-leave-to {
    opacity: 0;
    transform: translateX(24px);
  }

  .move-enter-to .inner,
  .move-leave .inner {
    transform: rotate(0);
  }

  .move-enter-active .inner,
  .move-leave-active .inner {
    transition: all 0.5s;
  }
  .move-enter .inner,
  .move-leave-to .inner {
    transform: rotate(360deg);
  }

  .iconfont {
    font-size: 0.533333rem;
    color: #3190e8;
  }
  .count {
    font-size: 0.373333rem;
    line-height: 0.546667rem;
    padding: 0 0.2rem;
  }
}
</style>